// pages/HADS/HADS.js
const db = wx.cloud.database();

Page({
    /**
     * 页面的初始数据
     */
    data: {
      qid:1,
      uid:1,
      name: "",
      questions:[],
      answers:[],
      resultShow: 0,
      score:0,
      resultSug:""
    },
    
    _addAnswer(newAnswer){
      let tmpAnswers = this.data.answers;
      const foundIndex = tmpAnswers.findIndex((item)=>{
        return item.id == newAnswer.id;
      });
  
      if(foundIndex !== -1){
        tmpAnswers.splice(foundIndex, 1, newAnswer);
      }else{
        tmpAnswers = [...tmpAnswers, newAnswer];
      }
  
      this.setData({
        answers:tmpAnswers,
      })
  
    },


    onRadioChange(event){
      // console.log(event)
      const qId = event.target.dataset.qid;
      const optionId = event.detail.value;
      
      const tmpQ = this.data.questions.find((item)=>{
        return item.id == qId;
      })
      // console.log(tmpQ);
  
      const tmpOption = tmpQ.options.find((item)=>{
        
        return item.option_id == optionId
      })
      // console.log(tmpOption)
  
      const newAnswer = {
        "id": qId,
        "options": [
          {
            "option_id": optionId,
            "option_value": tmpOption.option_value,
          }
        ],
      }
      this._addAnswer(newAnswer);
  
    },

    getHADS(qid){
      const that = this;

      db.collection('HADS_questions').where({
        id: qid
      })
      .get()
      .then(res => {
        console.log(res)
        that.setData({
          name: res.data[0].name,
          questions: res.data[0].questions,
        })
      })
      .catch(err => {
        console.error(err)
      })
    },

    submitHADS(){
      const that = this;
      var the_score = 0;
      var judge = -1;

      const answers = this.data.answers.sort((a,b)=>{
        return a.id - b.id;
      });

      //开始计算分数
      for (var i = 0;i< this.data.answers.length;i++){
        var optionId = this.data.answers[i].options[0].option_id;
        if (optionId == "A"){
            the_score = the_score+0;
        }
        else if (optionId == "B"){
            the_score = the_score+1;
        }
        else if (optionId == "C"){
            the_score = the_score+2;
        }
        else if (optionId == "D"){
            the_score = the_score+3;
        }
      }


      if (the_score>21){
        the_score = 21;
      }

      if (the_score<=7){
        this.setData({
          resultSug: "根据您的分数，您无抑郁症状",            
        });
      }

      else if(the_score<=10 && the_score>=8){
        this.setData({
          resultSug: "根据您的分数，您可能存在焦虑或抑郁",            
        });
      }

      else{       
        this.setData({
          resultSug: "根据您的分数，您肯定存在焦虑或抑郁，请及时前往就医。",            
        });
      }

      //检验是否做完题目
      if (this.data.answers.length<14 || this.data.answers.length>14){
        judge=0;
      }
      else if (this.data.answers.length=14){
        judge=1;
      }

      console.log("让我看看这个分数有没有",the_score)
      console.log("让我看看这个aaaaa",this.data.resultSug)
      if(judge == 1){
      wx.showModal({
        title: '测试结果',
        content: '您的测试分数是:'+the_score+'。\r\n'+this.data.resultSug,
        success: function (res) {
            if (res.confirm) {
                console.log('用户点击确定') 
            }else{
               console.log('用户点击取消')
            }
        }
      }) 
    }  
    else{
      wx.showModal({
        title: '未完成测试',
        content: '请您仔细检查是否还有题目没有完成哦',
        success: function (res) {
            if (res.confirm) {
                console.log('用户点击确定') 
            }else{
               console.log('用户点击取消')
            }
        }
      }) 
    }

      const objAnswer = {
        id:this.data.uid,
        score:the_score,
        answers:answers,
        judge:judge,
      }
    
      const strUid = 'uid_' + that.data.uid;

      db.collection('HADS_answers')
        .doc(strUid)
        .set({
          data: objAnswer,
        })
      .then((res)=>{

        that.setData({
          resultShow: !0,
          score:the_score         
        });
      })
      .catch(err=>{
        console.error(err);
        wx.showToast({
          title: '保存失败',
          icon: 'error',
          duration: 2000
        })      
      })
    },

    onLoad() {
      this.getHADS(this.data.qid);
    },

})

